package service.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import service.edu.entity.Subject;
import service.edu.entity.excel.SubjectData;
import service.edu.entity.subject.FirstSubject;
import service.edu.entity.subject.SecondSubject;
import service.edu.listener.SubjectExcelListener;
import service.edu.mapper.SubjectMapper;
import service.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author diao
 * @since 2020-08-04
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, SubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //显示课程列表
    @Override
    public List<FirstSubject> getAllSubjects() {
        //注:点击ServiceImpl发现里面帮我们自动注入了baseMapper,其可以调用dao层
        //其等同于this.list(firstQueryWrapper);

        //1.查询所有一级分类课程
        QueryWrapper<Subject> firstQueryWrapper = new QueryWrapper<>();
        firstQueryWrapper.eq("parent_id", "0");
        List<Subject> oneSubjectList = baseMapper.selectList(firstQueryWrapper);
        //2.查询所有二级分类课程
        QueryWrapper<Subject> secondQueryWrapper = new QueryWrapper<>();
        secondQueryWrapper.ne("parent_id", "0");
        List<Subject> twoSubjectList = baseMapper.selectList(secondQueryWrapper);
        //3.先建立方法返回的list
        List<FirstSubject> finalSubjectList=new ArrayList<>();
        //4.封装一级分类课程
        //遍历firstSubjectList,然后取其属性封装到finalSubjectList的元素中
        for (Subject oneSubject : oneSubjectList) {
            FirstSubject finalSubject = new FirstSubject();
            /*finalSubject.setId(subject.getId());
              finalSubject.setTitle(subject.getTitle());*/
            //该方法会将一个对象中的属性都get出来,然后set另一个对象属性中
            BeanUtils.copyProperties(oneSubject, finalSubject);

            //遍历twoSubjectList,然后取其整体封装到finalSubject中
            List<SecondSubject> secondSubjectList=new ArrayList<>();
            for (Subject twoSubject : twoSubjectList) {
                if (twoSubject.getParentId().equals(finalSubject.getId())){
                    SecondSubject secondSubject = new SecondSubject();
                    BeanUtils.copyProperties(twoSubject, secondSubject);
                    secondSubjectList.add(secondSubject);
                }
            }

          //将二级分类课程列表封装到一级分类课程的属性中
          finalSubject.setChildren(secondSubjectList);
          //将一级分类课程添加至列表中
          finalSubjectList.add(finalSubject);
        }
        //返回一级分类课程列表
        return finalSubjectList;
    }
}
